<template>
  <wx-tables
    :columns="columns"
    title="账号管理"
    :data-source="dataSource"
    :loading="loading"
    :total="total"
    :size.sync="size"
    :page.sync="page"
  >
    <!-- tools -->
    <template #tools>
      <el-button type="primary" class="table-btn" @click="handleOpenForm()">添加账号</el-button>
      <!--      <el-button class="table-btn" icon="el-icon-refresh-right" @click="handleGetTable">刷新</el-button>-->
    </template>
    <template v-slot:role="record">
      {{ ROlE_TYPE[record.row.role] || '无' }}
    </template>
    <template v-slot:oper="record">
      <div class="oper-box">
        <span><el-link type="primary" @click="handleChangePassWord(record.row.id)">重置密码</el-link></span>
        <el-popconfirm
          :ref="`popover-${record.$index}`"
          :title="`确定${record.row && record.row.status === 0 ? '启用':'注销'}该账号吗？`"
          @confirm="handleSetStatus(record.row)"
        >
          <el-link slot="reference" type="primary">{{ record.row && record.row.status === 0 ? '启用':'注销' }}</el-link>
        </el-popconfirm>
      </div>
    </template>
    <wx-dialog
      ref="wx-dialog"
      :visible.sync="formVisible"
      :is-add="isAdd"
      width="500px"
      @ok="handleSaveForm"
    >
      <div class="wx-dialog-form">
        <el-form ref="ruleForm" v-loading="formLoading" :rules="ruleForm" :model="formInfo" label-position="left" label-width="50px">
          <el-form-item label="角色" prop="role">
            <el-select v-model="formInfo.role" clearable placeholder="请选择角色">
              <el-option
                v-for="(item, type) of ROlE_TYPE"
                :key="type"
                :label="item"
                :value="Number(type)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="名称" prop="nick_name">
            <el-input v-model="formInfo.nick_name" style="width: 200px;" placeholder="请输入名称" />
          </el-form-item>
          <el-form-item label="账号" prop="account_admin">
            <el-input v-model="formInfo.account_admin" style="width: 200px;" placeholder="请输入账号" />
          </el-form-item>
        </el-form>
      </div>
    </wx-dialog>
  </wx-tables>
</template>

<script>
import { get, post } from '@/utils/request'
import md5 from 'js-md5'
import { modifySelfPassword } from '@/api/common'
import WxDialog from '@/components/WxDialog'
import WxTables from '@/components/WxTables'
import TableMixins from '@/components/WxTables/mixins'
import { ROlE_TYPE } from '@/views/constants'
export default {
  name: 'Account',
  components: {
    WxDialog,
    WxTables
  },
  mixins: [TableMixins],
  data() {
    return {
      pwdDialog: false,
      columns: [
        { config: { type: 'index', width: 100 }, label: '序号' },
        { dataIndex: 'nick_name', label: '名称' },
        { dataIndex: 'account_admin', label: '账号' },
        { dataIndex: 'role', label: '角色', slots: true },
        { dataIndex: 'created_at', label: '创建时间' },
        { dataIndex: 'oper', label: '操作', slots: true }
      ],
      markId: 'id',
      // 查询表格API
      api: 'api/adminPortal/accountAdmin/getListByCon',
      // 根据ID markId 查询
      // 接口前缀 修改
      baseApi: 'api/adminPortal/accountAdmin/',
      ruleForm: {
        role: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ],
        nick_name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        account_admin: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ]
      },
      ROlE_TYPE,
      formInfo: {
        type: '管理员',
        name: '',
        account_number: ''
      }
    }
  },
  methods: {
    handleSetStatus(data) {
      console.log(data)
      var { id, status } = data
      switch (status) {
        case 0:status = 1; break
        case 1:status = 0
      }
      get('api/adminPortal/accountAdmin/setStatus', {
        id, status
      }).then((res) => {
        if (res.code === 200) {
          this.$message(res.msg)
          this.handleGetTable()
        }
      })
    },
    handleChangePassWord(id) {
      post('api/adminPortal/accountAdmin/resetPassword', { id }).then((res) => { if (res.code === 200) { this.$message('成功') } })
    },
    handleResetPwd() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const newForm = JSON.parse(JSON.stringify(this.formInfo))
          Object.keys(newForm).forEach((key) => {
            if (key !== 'account_admin') {
              newForm[key] = md5(newForm[key])
            }
          })
          modifySelfPassword(newForm).then((res) => {
            if (res.code === 200) {
              this.pwdDialog = false
              this.$message.success('修改成功')
              this.formInfo = {}
            }
          })
        } else {
          this.$refs.myDialog.disabled = false
        }
      })
    }
  }
}
</script>
<style lang="scss" src="./style/index.scss" scoped/>
